package com.heima.comment.service.impl;


import com.heima.api.auth.IAuthClient;
import com.heima.comment.pojo.ArticleComment;
import com.heima.comment.pojo.ReplyComment;
import com.heima.comment.service.IReplyCommentService;
import com.heima.model.comment.dtos.CommentDto;
import com.heima.model.comment.dtos.QueryCommentDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.utils.common.UserIdThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Xanny
 * @Data 2024/8/23 10:17
 * @Version 1.0
 */
@Service
public class ReplyCommentServiceImpl implements IReplyCommentService {
    @Autowired
    private IAuthClient authClient;

    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public ResponseResult loadUserComment(QueryCommentDto dto) {
        Criteria criteria = new Criteria();

        //如果是第一次加载
        if (dto.getMinDate().compareTo(new Date(20000000000000L)) == 0){
            criteria = Criteria.where("commentId").is(dto.getCommentId())
                    .and("publishTime").lt(dto.getMinDate());
        }else{
            criteria = Criteria.where("commentId").is(dto.getCommentId())
                    .and("publishTime").gt(dto.getMinDate());
        }


        Query query =Query.query(criteria)
                .with(Sort.by(Sort.Direction.DESC, "publishTime"));
        List<ReplyComment> replyCommentList = mongoTemplate.find(query, ReplyComment.class);

        return ResponseResult.okResult(replyCommentList);
    }

    /**
     * 新增回复
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveUserComment(CommentDto dto) {
        //1.完成评论的回复
        ReplyComment replyComment = new ReplyComment();
        replyComment.setCommentId(dto.getCommentId());
        replyComment.setContent(dto.getContent());

        replyComment.setAuthorId(Long.valueOf(UserIdThreadLocal.getUserId()));
        String name = authClient.getById(Long.valueOf(UserIdThreadLocal.getUserId()));
        replyComment.setAuthorName(name);
        replyComment.setPublishTime(new Date());
        replyComment.setCreatedTime(LocalDateTime.now());
        mongoTemplate.save(replyComment);

        //2.完成评论的回复数量+1
        Query query = Query.query(Criteria.where("_id").is(dto.getCommentId()));
        Update update = new Update();
        update.inc("reply",1);
        mongoTemplate.updateFirst(query, update, ArticleComment.class);

        return ResponseResult.okResult(null);
    }

    /**
     * 点赞
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likeUserComment(CommentDto dto) {
        if (dto.getOperation() == 0){
            //1.1点赞 更新该评论的点赞数量
            Query query = Query.query(Criteria.where("_id").is(dto.getCommentRepayId()));
            Update update = new Update();
            update.inc("likes",1);
            mongoTemplate.updateFirst(query, update, ReplyComment.class);
        }else{
            //1.2取消点赞 更新该评论的点赞数量
            Query query = Query.query(Criteria.where("_id").is(dto.getCommentRepayId()));
            Update update = new Update();
            update.inc("likes",-1);
            mongoTemplate.updateFirst(query, update, ReplyComment.class);
        }

        //2.更新该用户的的点赞状态
        Query query =Query.query(Criteria.where("_id").is(dto.getCommentRepayId()));
        Update update = new Update();
        update.set("operation",dto.getOperation());
        mongoTemplate.updateFirst(query, update, ReplyComment.class);


        //3.查询点赞数并返回
        ReplyComment one = mongoTemplate.findOne(Query.query(Criteria.where("_id").is(dto.getCommentRepayId())), ReplyComment.class);

        Map<String,Integer> data = new HashMap();
        data.put("likes",one.getLikes());
        return ResponseResult.okResult(data);
    }
}
